#coding=utf8
import pymysql

host    = "127.0.0.1"
user    = 'root'
passwd  = ''
database= "test"
port    = 3306

#主要是pymysql

# 链接mysql 
def connect_msyql():
    conn = pymysql.connect(host,user,passwd,database,port)
    cursor = conn.cursor()
    return conn,cursor

#关闭mysql链接
def close_mysql(cursor,conn):
    cursor.close()
    conn.close()


#查询一条数据
def get_one_data(sql_cmd):
    cursor,conn = connect_msyql()
    cursor.execute(sql_cmd)
    data = cursor.fetchone()
    close_mysql(cursor, conn)
    return data


#查询多条数据
def get_muliti_data(sql_cmd):
    cursor,conn = connect_msyql()
    cursor.execute(sql_cmd)
    data = cursor.fetchall()
    close_mysql(cursor, conn)
    return data

#插入和更新还有删除一条或多条数据
def insert_or_updata_or_delete_data(sql_cmd):
    cursor,conn = connect_msyql()
    result = cursor.execute(sql_cmd)
    conn.commit()
    close_mysql(cursor, conn)
    return result

# sql = "select * from stu where name=%s" 查询一条数据
# sql = "delete from stu where name = %s" 删除数据
# sql = "insert into stu(id,name,age) VALUE (%s,%s,%s)"  添加一条数据
# sql = "insert into stu(id,name,age) VALUES (%s,%s,%s),(%s,%s,%s)"  添加多条数据